package com.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.dao.SMSDao;
import com.db.DBManager;
import com.domain.ClassInfo;
import com.domain.Model;
import com.domain.SMSBook;
import com.domain.SMSInfo;
import com.domain.SMSModel;

@Repository("smsDao")
public class SMSDaoImpl extends BaseDaoImpl implements SMSDao {

	/**
	 * 查询数据
	 */
	public Object queData(String url) {

		ResultSet result = null;
		List<SMSInfo> list = new ArrayList<SMSInfo>();
		try {
			Connection conn = DBManager.getConn();
			PreparedStatement ps = conn.prepareStatement(url);
			result = ps.executeQuery();
			SMSInfo sms = new SMSInfo();
			while (result != null && result.next()) {
				String id = result.getString(sms.getId());
				String teamName = result.getString(sms.getTeamName());
				String phoneNum = result.getString(sms.getPhoneNum());
				String contents = result.getString(sms.getContents());
				String createDate = result.getString(sms.getCreateDate());
				SMSInfo smsInfo = new SMSInfo(id, teamName, phoneNum, contents,
						createDate);
				list.add(smsInfo);
			}
			DBManager.close(result, ps, conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 查询短信通讯录
	 * 
	 * @param url
	 * @return
	 */
	public Object queSMSBook(String url) {

		ResultSet result = null;
		List<SMSBook> list = new ArrayList<SMSBook>();
		try {
			Connection conn = DBManager.getConn();
			PreparedStatement ps = conn.prepareStatement(url);
			result = ps.executeQuery();
			SMSBook sms = new SMSBook();
			while (result != null && result.next()) {
				String id = result.getString(sms.getId());
				String name = result.getString(sms.getName());
				String phoneNum = result.getString(sms.getPhoneNum());
				String role = result.getString(sms.getRole());
				SMSBook smsBook = new SMSBook(id, name, phoneNum, role);
				list.add(smsBook);
			}
			DBManager.close(result, ps, conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// 获取表总记录数
	public Object getDataCount(String url) {

		ResultSet result = null;
		int count = 0;
		try {
			Connection conn = DBManager.getConn();
			PreparedStatement ps = conn.prepareStatement(url);
			result = ps.executeQuery();
			while (result != null && result.next()) {
				count = result.getInt(1);
			}
			DBManager.close(result, ps, conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	// 分页查询数据
	public Object queAddress(String url) {

		ResultSet result = null;
		List<ClassInfo> list = new ArrayList<ClassInfo>();
		try {
			Connection conn = DBManager.getConn();
			PreparedStatement ps = conn.prepareStatement(url);
			result = ps.executeQuery();
			ClassInfo cInfo = new ClassInfo();
			while (result != null && result.next()) {
				String classId = result.getString(cInfo.getClassid());
				String name = result.getString(cInfo.getName());
				ClassInfo cInfoT = new ClassInfo(classId, name);
				list.add(cInfoT);
			}
			DBManager.close(result, ps, conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 查询model数据
	 * 
	 * @param url
	 * @return
	 */
	public Object queModel(String url) {

		ResultSet result = null;
		List<Model> list = new ArrayList<Model>();
		try {
			Connection conn = DBManager.getConn();
			PreparedStatement ps = conn.prepareStatement(url);
			result = ps.executeQuery();
			Model model = new Model();
			while (result != null && result.next()) {
				String name = result.getString(model.getName());
				String content = result.getString(model.getValue());
				Model modelT = new Model(name, content);
				list.add(modelT);
			}
			DBManager.close(result, ps, conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// 分页查询数据
	public Object queAddressInfo(String url) {

		ResultSet result = null;
		StringBuilder sb = new StringBuilder();
		try {
			Connection conn = DBManager.getConn();
			PreparedStatement ps = conn.prepareStatement(url);
			result = ps.executeQuery();
			while (result.next()) {
				sb.append(result.getString(1));
			}
			DBManager.close(result, ps, conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	// 分页查询数据
	public Object getSMSModel(String url) {
		ResultSet result = null;
		List<SMSModel> list = new ArrayList<SMSModel>();
		try {
			Connection conn = DBManager.getConn();
			PreparedStatement ps = conn.prepareStatement(url);
			result = ps.executeQuery();
			SMSModel sms = new SMSModel();
			while (result != null && result.next()) {
				String id = result.getString(sms.getId());
				String name = result.getString(sms.getName());
				String contents = result.getString(sms.getContents());
				String createDate = result.getString(sms.getCreateDate());
				SMSModel smsModel = new SMSModel(id, name, contents, createDate);
				list.add(smsModel);
			}
			DBManager.close(result, ps, conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 获取短信数量
	 */
	public int getSMSCount(String url) {
		ResultSet result = null;
		int count = 0;
		try {
			Connection conn = DBManager.getConn();
			PreparedStatement ps = conn.prepareStatement(url);
			result = ps.executeQuery();
			if (result.next()) {
				System.out.println("result:" + result);
				count = result.getInt(1);
			}
			DBManager.close(null, ps, conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}
}
